package caidan;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import caidan.res.Resource;
import jinyilw.common.Operator;
import jinyilw.swing.FontTools;
import jinyilw.swing.visuals.FlatDialog;
import jinyilw.swing.visuals.Jcomp.TransRichButton;
import jinyilw.swing.visuals.comp.ActImageComp;

public class LagerView extends FlatDialog
{
	private static final long serialVersionUID = -7195737034738553648L;
	private final TransRichButton choose = new TransRichButton();
	private final TransRichButton close = new TransRichButton("关闭");
	private final ActImageComp previousComp = new ActImageComp(
			Resource.arrowIcon2);
	private final ActImageComp nextComp = new ActImageComp(
			Resource.arrowIcon1);
	private final JLabel messegeLabel = new JLabel();
	private FoodPanel foodPanel;

	public LagerView(JFrame jf)
	{
		super(jf);
		setDefaultDimensionRatio(0.6, 0.75);
		Dimension d = getDefaultDimension();
		if (d.width > 800)
			setSize(800, 600);
		else
		{
			int h = (int) (d.width * 0.75);
			if (d.height > h)
				d.height = h;
			else
				d.width = (int) (d.height / 0.75);
			setSize(d.width, d.height);
		}
		setCustomAlpha(1f);
		setTitleHeight(36);
		setTitleFont(FontTools.getCustomFont(19));
		setLocationRelativeTo(null);
		Container contentPanel = getContentPane();
		contentPanel.setLayout(null);
		previousComp.addClickOperator(new PreviousOperator());
		contentPanel.add(previousComp);
		nextComp.addClickOperator(new NextOperator());
		contentPanel.add(nextComp);
		messegeLabel.setFont(FontTools.getBigFont());
		messegeLabel.setForeground(Color.red);
		messegeLabel.setHorizontalAlignment(SwingConstants.CENTER);
		contentPanel.add(messegeLabel);

		choose.setFont(FontTools.getBigFont());
		choose.setForeground(Color.orange);
		contentPanel.add(choose);
		choose.addActionListener((ActionEvent e) ->
		{
			dispose();
			if (foodPanel.isEditable())
				foodPanel.deleteThis();
			else
				foodPanel.chooseAct();
		});
		close.setFont(FontTools.getBigFont());
		close.setForeground(Color.orange);

		contentPanel.add(close);
		close.addActionListener((ActionEvent e) ->
		{
			dispose();
		});

		//		KeyboardFocusManager.getCurrentKeyboardFocusManager()
		//				.addKeyEventDispatcher(new KeyEventDispatcher()
		//				{
		//					@Override
		//					public boolean dispatchKeyEvent(KeyEvent e)
		//					{
		//						// System.out.println(e.getID());
		//						if (isActive() && e.getID() == KeyEvent.KEY_PRESSED)
		//						{
		//							int keyCode = e.getKeyCode();
		//							if (keyCode == KeyEvent.VK_RIGHT)
		//								nextAct();
		//							else if (keyCode == KeyEvent.VK_LEFT)
		//								previousAct();
		//						}
		//						return true;
		//					}
		//				});

		addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				int keyCode = e.getKeyCode();
				if (keyCode == KeyEvent.VK_RIGHT)
					nextAct();
				else if (keyCode == KeyEvent.VK_LEFT)
					previousAct();
				// System.out.println(keyCode);
			}
		});
	}

	@Override
	public void setVisible(boolean b)
	{
		super.setVisible(b);
		if (b)
			requestFocus();
	}

	@Override
	public void setSize(int width, int height)
	{
		height = width * 7 / 10;
		super.setSize(width, height);
		int ww = width / 7;
		previousComp.setLocation(ww, height - 115);
		nextComp.setLocation(width - ww - nextComp.getWidth(), height - 115);
		messegeLabel.setBounds(0, height / 2 - 30, width, 40);
		choose.setBounds(width / 2 - 105, height - 120, 90, 75);
		close.setBounds(width / 2 + 15, height - 120, 90, 75);
	}

	public void setView(FoodPanel fp)
	{
		foodPanel = fp;
		setTitle(fp.getFoodName());
		Image image = fp.getFoodImage();
		if (image == null)
			messegeLabel.setText(fp.getImageNotFindMessege());
		else
			messegeLabel.setText("");
		setCustomBGImage(image);
		if (foodPanel.isEditable())
		{
			choose.setText("删除");
			choose.setEnabled(true);
		} else if (foodPanel.isChoosed())
		{
			choose.setText("取消选择");
			choose.setEnabled(true);
		} else
		{
			choose.setText("选择");
			if (foodPanel.isMaxChoosed())
				choose.setEnabled(false);
			else
				choose.setEnabled(true);
		}
	}

	public void update()
	{
		if (foodPanel != null)
			setView(foodPanel);
	}

	public void nextAct()
	{
		ArrayList<FoodPanel> foodPanels = foodPanel.getCurrentList();
		int index = (foodPanels.indexOf(foodPanel) + 1) % foodPanels.size();
		FoodPanel fp = foodPanels.get(index);
		if (fp != null)
			setView(fp);
	}

	public void previousAct()
	{
		ArrayList<FoodPanel> foodPanels = foodPanel.getCurrentList();
		int index = foodPanels.indexOf(foodPanel) - 1;
		if (index == -1)
			index = foodPanels.size() - 1;
		FoodPanel fp = foodPanels.get(index);
		if (fp != null)
			setView(fp);
	}

	private class NextOperator implements Operator
	{
		@Override
		public void operate(Object o)
		{
			nextAct();
		}
	}

	private class PreviousOperator implements Operator
	{
		@Override
		public void operate(Object o)
		{
			previousAct();
		}
	}
}
